<?php
/**
 * @title 138. 随机链表的复制
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a Node.
 * class Node {
 *     public $val = null;
 *     public $next = null;
 *     public $random = null;
 *     function __construct($val = 0) {
 *         $this->val = $val;
 *         $this->next = null;
 *         $this->random = null;
 *     }
 * }
 */

class Solution {
    /**
     * @param Node $head
     * @return Node
     */
    function copyRandomList($head) {
        /**
         * @since 2023-12-27 新建链表
         */
        $newHead = $newNode = new Node();
        $node = $head;

        /**
         * @since 2023-12-27 哈希记录每个链表的节点
         */
        $hashArray = [];

        /**
         * @since 2023-12-27 第一次循环复制链表的值
         */
        while($node) {
            /**
             * @since 2023-12-27 赋值，哈希记录节点
             */
            $newNode->val = $node->val;
            $hashArray[$node->val] = $newNode;

            /**
             * @since 2023-12-27 Node还有下一个节点，建Node节点
             */
            $node = $node->next;
            if ($node) {
                $newNode->next = new Node();
                $newNode = $newNode->next;
            } else {}
        }

        /**
         * @since 2023-12-27 第二次循环复制链表的随机节点
         */
        $node = $head;
        $newNode = $newHead;
        while($node) {
            /**
             * @since 2023-12-27 指向random节点
             */
            if($node->random !== null) {
                $newNode->random = $hashArray[$node->random->val];
            } else {}

            /**
             * @since 2023-12-27 继续下一个节点
             */
            $node = $node->next;
            $newNode = $newNode->next;
        }

        /**
         * @since 2023-12-27 返回新链表
         * @return Node
         */
        return $newHead;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.27_2.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
